package com.ng.common.component.engine.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 数据脱敏工具类
 * 提供各种常用的数据脱敏方法
 */
public final class DesensitizationUtil {
    
    private static final char MASK_CHAR = '*';
    private static final String MASK_4 = "****";
    private static final String MASK_8 = "********";
    
    // 私有构造函数，防止实例化
    private DesensitizationUtil() {
        throw new UnsupportedOperationException("Utility class cannot be instantiated");
    }

    /**
     * 手机号码脱敏
     * 保留前3位和后4位，中间用4个*代替
     */
    public static String maskPhoneNumber(String phoneNumber) {
        if (StringUtils.isEmpty(phoneNumber)) {
            return phoneNumber;
        }
        return phoneNumber.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1" + MASK_4 + "$2");
    }

    /**
     * 身份证号脱敏
     * 保留前6位和后4位，中间用8个*代替
     */
    public static String maskIdCard(String idCard) {
        if (StringUtils.isEmpty(idCard)) {
            return idCard;
        }
        return idCard.replaceAll("(\\d{6})\\d{8}(\\d{4})", "$1" + MASK_8 + "$2");
    }

    /**
     * 银行卡号脱敏
     * 仅显示后4位，其余用*代替
     */
    public static String maskBankCard(String bankCard) {
        if (StringUtils.isEmpty(bankCard)) {
            return bankCard;
        }
        return bankCard.replaceAll("\\d+(?=\\d{4})", StringUtils.repeat(MASK_CHAR, bankCard.length() - 4));
    }

    /**
     * 邮箱地址脱敏
     * 仅显示首字符和@后的内容
     */
    public static String maskEmail(String email) {
        if (StringUtils.isEmpty(email)) {
            return email;
        }
        return email.replaceAll("(^[a-zA-Z0-9]{1})[a-zA-Z0-9]+(@.*)", "$1" + MASK_4 + "$2");
    }

    /**
     * 姓名脱敏
     * 仅显示姓，其他用*代替
     */
    public static String maskName(String name) {
        if (StringUtils.isEmpty(name)) {
            return name;
        }
        if (name.length() <= 1) {
            return name;
        }
        return name.substring(0, 1) + StringUtils.repeat(MASK_CHAR, name.length() - 1);
    }

    /**
     * 地址脱敏
     * 保留前6个字符，其余用*代替
     */
    public static String maskAddress(String address) {
        if (StringUtils.isEmpty(address)) {
            return address;
        }
        if (address.length() <= 6) {
            return address;
        }
        return address.substring(0, 6) + StringUtils.repeat(MASK_CHAR, 6);
    }

    /**
     * 自定义位置脱敏
     * 对指定起始和结束位置进行脱敏处理
     */
    public static String maskBetween(String text, int start, int end) {
        if (StringUtils.isEmpty(text)) {
            return text;
        }
        if (start < 0 || end >= text.length() || start > end) {
            return text;
        }
        
        StringBuilder sb = new StringBuilder(text);
        for (int i = start; i <= end; i++) {
            sb.setCharAt(i, MASK_CHAR);
        }
        return sb.toString();
    }

    /**
     * 保留指定数量的字符，其余脱敏
     * @param text 原文本
     * @param prefixLength 保留前缀长度
     * @param suffixLength 保留后缀长度
     */
    public static String maskWithFix(String text, int prefixLength, int suffixLength) {
        if (StringUtils.isEmpty(text)) {
            return text;
        }
        
        if (prefixLength < 0 || suffixLength < 0 || prefixLength + suffixLength >= text.length()) {
            return text;
        }

        String prefix = text.substring(0, prefixLength);
        String suffix = text.substring(text.length() - suffixLength);
        String mask = StringUtils.repeat(MASK_CHAR, text.length() - prefixLength - suffixLength);
        
        return prefix + mask + suffix;
    }
    
    public static void main(String[] args) {
        // 测试各种脱敏场景
        String phoneNumber = "13812345678";
        String idCard = "310123199001011234";
        String bankCard = "6222021234567890123";
        String email = "user@example.com";
        String name = "张三丰";
        String address = "上海市浦东新区张江高科技园区";

        System.out.println("手机号码脱敏: " + DesensitizationUtil.maskPhoneNumber(phoneNumber));
        System.out.println("身份证号脱敏: " + DesensitizationUtil.maskIdCard(idCard));
        System.out.println("银行卡号脱敏: " + DesensitizationUtil.maskBankCard(bankCard));
        System.out.println("邮箱地址脱敏: " + DesensitizationUtil.maskEmail(email));
        System.out.println("姓名脱敏: " + DesensitizationUtil.maskName(name));
        System.out.println("地址脱敏: " + DesensitizationUtil.maskAddress(address));
        
        // 自定义脱敏
        String customText = "这是一段需要部分脱敏的文本";
        System.out.println("自定义脱敏1: " + DesensitizationUtil.maskBetween(customText, 3, 8));
        System.out.println("自定义脱敏2: " + DesensitizationUtil.maskWithFix(customText, 2, 2));
    }
}